/******************************************************************************
@file:  SpecialOffer.java
@author Ramesh Anandhan

DESCRIPTION
-----------------------------------------------------------------------------
Copyright (c) 2011, GEEKWAVE SOLUTIONS INC.

All rights reserved.
******************************************************************************/

package com.smartc.soffer;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

import com.smartc.connection.HttpConnection;
import com.smartc.health.GymYogaRegistration;
import com.smartc.ui.AccountLogin;
import com.smartc.ui.R;

public class SpecialOffer extends Activity {

	protected static final String TAG = "SpecialOffer";
	private final int PROGRESS_DLG = 0x01;
	private ArrayList<Object> itemList;
	private ItemBean bean;

	ListView mItemListView;
	ListViewAdapter adapter;
	private Spinner mCity = null;
	private String mSelectedCity = null;
	private HttpConnection mConnection;
	private ResponseHandler mResponse = new ResponseHandler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.special_offer);
		mConnection = new HttpConnection();
		mConnection.AddHandler(mResponse);
		mCity = (Spinner) findViewById(R.id.offer_city);
		
		mCity.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				if (AccountLogin.DEBUG)
					Log.i(TAG, "onItemSelected() position:" + position);

				if (position == 0)
					return;
				mSelectedCity = mResponse.mCity_Id.get(--position);

				String domain = getResources().getString(R.string.domain);
				String query = getResources().getString(
						R.string.url_special_offer);
				String url = domain + query;

				mConnection.setUrl(url);
				mConnection.clearParam();
				if (AccountLogin.DEBUG)
					Log.i(TAG, "onItemSelected() mSelectedCity:" + mSelectedCity);
				mConnection.AddParam("idCity", mSelectedCity);
				mConnection.Execute();
				showDialog(PROGRESS_DLG);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}

		});
	}
	
	@Override
	protected Dialog onCreateDialog(int id, Bundle args) {
		if (AccountLogin.DEBUG)
			Log.i(TAG, "onCreateDialog id" + id);
		switch (id) {	
		case PROGRESS_DLG:
			ProgressDialog mDialog = new ProgressDialog(this);
			mDialog.setMessage("Request In progress....");
			mDialog.setIndeterminate(true);
			mDialog.setCancelable(true);
			return mDialog;
		default:
			return super.onCreateDialog(id, args);
		}
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		String domain = getResources().getString(R.string.domain);
		String city_query = getResources().getString(
				R.string.bus_register_city_query);
		String city_url = domain + city_query;
		Log.i(TAG, "onStart city_url:" + city_url);
		mConnection.setUrl(city_url);
		mConnection.clearParam();
		mConnection.AddHandler(mResponse);
		mConnection.Execute();
	}

	public class ResponseHandler extends DefaultHandler {

		private boolean mCity = false;
		private boolean idCity = false;
		private boolean txtOffer = false;
		public ArrayList<String> mCity_List = new ArrayList<String>();
		public ArrayList<String> mCity_Id = new ArrayList<String>();
		public ArrayList<String> mOffer_List = new ArrayList<String>();
		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			if (this.mCity) {
				mCity_List.add(new String(ch, start, length));
			}else if (this.idCity) {
				mCity_Id.add(new String(ch, start, length));
			}else if(this.txtOffer){
				mOffer_List.add(new String(ch, start, length));
			}
		}

		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			if (localName.equals("nmCity")) {
				this.mCity = false;
			}else if (localName.equals("idCity")) {
				this.idCity = false;
			}else if (localName.equals("Cities")) {
				// this.mCityMain = false;
				Message myMessage = new Message();
				myMessage.obj = "CITY";
				handler.sendMessage(myMessage);
			}else if(localName.equals("SpecialOffer")){
				Message myMessage = new Message();
				myMessage.obj = "OFFER";
				handler.sendMessage(myMessage);
			}else if(localName.substring(0, localName.length()-1).equals("txtOffer")){
				this.txtOffer = false;
			}
		}

		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			if (localName.equals("nmCity")) {
				this.mCity = true;
			} else if (localName.equals("idCity")) {
				this.idCity = true;
			}else if (localName.equals("Cities")) {
				// this.mCityMain = true;
				mCity_List.clear();
				mCity_Id.clear();
				mCity_List.add("select city");
			}else if(localName.equals("SpecialOffer")){
				mOffer_List.clear();
			}else if(localName.substring(0, localName.length()-1).equals("txtOffer")){
				this.txtOffer = true;
			}
		}
	}
	
	public static class ViewHolder {
		TextView ItemDetails;
	}

	private class ListViewAdapter extends BaseAdapter {

		private LayoutInflater inflater;
		// private Context mContext;
		ArrayList<Object> mItemList;

		ListViewAdapter(Context context, ArrayList<Object> itemList) {
			super();
			// mContext = context;
			mItemList = itemList;
			this.inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return mItemList.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.offer_list, null);

				holder.ItemDetails = (TextView) convertView
						.findViewById(R.id.offer_desc);

				convertView.setTag(holder);
			} else
				holder = (ViewHolder) convertView.getTag();

			ItemBean bean = (ItemBean) itemList.get(position);

			holder.ItemDetails.setText(bean.getItemDesc());
			return convertView;
		}
	}

	public class ItemBean {
		String mItemdesc;

		public String getItemDesc() {
			return mItemdesc;
		}

		public void setItemDesc(String desc) {
			this.mItemdesc = desc;
		}

	}

	public void AddObjectToList(String desc) {

		bean = new ItemBean();
		bean.setItemDesc(desc);

		itemList.add(bean);
	}


	private Handler handler = new Handler() {
		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {
			if (AccountLogin.DEBUG)
				Log.i(TAG, "handleMessage msg:" + (String) msg.obj);
			String loginmsg = (String) msg.obj;
			if (loginmsg.equals("CITY")) {
				int count = mResponse.mCity_List.size();
				String city[] = new String[count];
				for (int i = 0; i < count; i++)
					city[i] = mResponse.mCity_List.get(i);

				ArrayAdapter Adapter = new ArrayAdapter(SpecialOffer.this,
						R.layout.simple_spinner_item, city);
				mCity.setAdapter(Adapter);
			}else if (loginmsg.equals("OFFER")) {
				removeDialog(PROGRESS_DLG);
				if (AccountLogin.DEBUG)
					Log.i(TAG, "handleMessage offer count" + mResponse.mOffer_List.size());
				itemList = new ArrayList<Object>();
				for (int i = 0; i < mResponse.mOffer_List.size(); i++) {
					AddObjectToList(mResponse.mOffer_List.get(i));
				}

				mItemListView = (ListView) findViewById(R.id.offer_list);
				adapter = new ListViewAdapter(SpecialOffer.this
						.getApplicationContext(), itemList);
				mItemListView.setAdapter(adapter);
			}
		}
	};

}
